var orderNum = 0;
var cus_lat = new Array;
var cus_lng = new Array;
var cus_id = new Array;
var cus_nickname = new Array;
var cus_status = new Array;
var INIFORCUS = true;
var INIFORCUS2 = true;
var ADDINDEX = 0;
var cus_location = new Array;
var cus_markers = new Array;
var markerNum = 0;
var cus_icon = new google.maps.MarkerImage(
		'../images/taxi.png', new google.maps.Size(48,
				48));
var map;
var tempCusLat;
var tempCusLng;
function testAddCus(map){
	map = map;
	//var interv2 = setInterval(updateCusMarkers, 5000);
	cus_lat = [39.89454, 39.8703, 39.98215],
	cus_lng = [116.31412, 116.46127, 116.27049]
	//setInterval(initCusMarkers_test, 6000);
	
	setInterval(function(){
		ADDINDEX = ADDINDEX % 3;
		cus_lat[markerNum] = cus_lat[ADDINDEX] + Math.random()*0.06;
		cus_lng[markerNum] = cus_lng[ADDINDEX] + Math.random()*0.06;
		tempCusLat = cus_lat[markerNum];
		tempCusLng = cus_lng[markerNum];
		
		cus_location[markerNum] = new google.maps.LatLng(tempCusLat, tempCusLng);
		cus_markers[markerNum] = new google.maps.Marker({
			position : cus_location[markerNum],
			title : 'hello',
			map : map,
			icon : cus_icon
		});	
		ADDINDEX++;
		markerNum++;
	},6000)

}
function updateCusMarkers(){
	var result = $.ajax({
		url : '/getOrderInfo',
		type  : 'post',
		dataType : "json",
		contentType : "application/json",
		data : JSON.stringify({}),
		success : function(data){
//			if(INIFORCUS){
//				orderNum = data.length;
//				initCusMarkers(data);
//				INIFORCUS = false;
//			}
//			updateNewCus(data);
		},
		error : function(data){
			alert(JSON.stringify(data));
			}
		});
}
function initCusMarkers_test(){
	ADDINDEX = ADDINDEX % 3;
	cus_lat[markerNum] = cus_lat[ADDINDEX] + Math.random()*0.06;
	cus_lng[markerNum] = cus_lng[ADDINDEX] + Math.random()*0.06;
	tempCusLat = cus_lat[markerNum];
	tempCusLng = cus_lng[markerNum];
	
	cus_location[markerNum] = new google.maps.LatLng(tempCusLat, tempCusLng);
	cus_markers[markerNum] = new google.maps.Marker({
		position : cus_location[markerNum],
		title : 'hello',
		map : map,
		icon : cus_icon
	});
	ADDINDEX++;
	markerNum++;
}
function initCusMarkers(data){
	var cus_icon = new google.maps.MarkerImage(
			'../images/taxi.png', new google.maps.Size(48,
					48));
	var markers = new Array();	
	
    getInfoArray2(data, 'order', 'cus_lat');
    getInfoArray2(data, 'order', 'cus_lng');
    getInfoArray2(data, 'order', 'cus_id');
    getInfoArray2(data, 'order', 'cus_nickname');
    getInfoArray2(data, 'order', 'cus_status');
	
    //ADD CUS_MARKERS!
    for(var index in cus_lat){
    	cus_location[index] = new google.maps.LatLng(cus_lat[index], cus_lng[index]);
    	cus_markers[index] = new google.maps.Marker({
    		position : cus_location[index],
    		title : 'hello',
    		map : map,
    		icon : cus_icon
    	});
    }
	setAllMap(cus_markers, map);	
	//ADD CONTENT!
	createCusContent('first','0');
}
//UPDATE THE CUS_CONTENT   
function createCusContent(type,newCusStaus){
	var cus_content = new Array;
	var cus_infowidow = new Array;
	if(type == first){
		for (var index in cus_lat){
		    cus_content[index] = '<div id="content">'+
		      '<div id="siteNotice">'+
		      '</div>'+
		      '<h1 id="firstHeading" class="firstHeading">DriverInfo</h1>'+
		      '<div id="bodyContent">'+
		      '<p><b>cus_id: </b>' + cus_id[index] +',</b></p>'+
		      '<p><b>cus_name: </b>' + cus_name[index] +',</b></p>'+
		      '<p><b>cus_status: </b>' + cus_status[index] +',</b></p>'+
		      '</div>'+
		      '</div>';
		     cus_infowidow[index] = new google.maps.InfoWindow({
				content: cus_content[index]
			});
			google.maps.event.addListener(cus_markers[index], 'click', function(){
				cus_infowidow[index].open(map, cus_markers[index])
			});
		}
	}
	else{
		var index;
		//only update related index;
		for(var i = 0; i < newCusStaus.length; i++){
			index = newCusStaus[i].index;
		    cus_content[index] = '<div id="content">'+
		      '<div id="siteNotice">'+
		      '</div>'+
		      '<h1 id="firstHeading" class="firstHeading">DriverInfo</h1>'+
		      '<div id="bodyContent">'+
		      '<p><b>cus_id: </b>' + cus_id[index] +',</b></p>'+
		      '<p><b>cus_name: </b>' + cus_name[index] +',</b></p>'+
		      '<p><b>cus_status: </b>' + cus_status[index] +',</b></p>'+
		      '</div>'+
		      '</div>';
		     cus_infowidow[index] = new google.maps.InfoWindow({
				content: cus_content[index]
			});
			google.maps.event.addListener(cus_markers[index], 'click', function(){
				cus_infowidow[index].open(map, cus_markers[index])
			});
		}
	}
}
function updateNewCus(data){
    var newCusStaus = [];
    var i = 0;
	for(var index in cus_status){
		// ?? new in forehead ??
		if(data[index].status != cus_status[index]){
			//??? is it right ???
			newCusStaus[i].index = index;
			newCusStaus[i].cus_status = data[index].status;
			i++;
			createCusContent('not first',newCusStaus);
		}
	}	
	if(data.length != orderNum){
		//???deleate before???
		//ADD MARKERS
		var diff = data.length - orderNum;
	    for(var index = orderNum; index < data.length; index++){
	    	cus_location[index] = new google.maps.LatLng(cus_lat[index], cus_lng[index]);
	    	cus_markers[index] = new google.maps.Marker({
	    		position : cus_location[index],
	    		title : 'hello',
	    		map : map,
	    		icon : cus_icon
	    	});
	    }
		setAllMap(cus_markers, map);
		
		//ADD CONTENT
		i = 0;
		for(var index = orderNum; index < data.length; index++){
			
			 newCusStaus[i].index = index;
			 newCusStaus[i].cus_status = data[index].status;
			 newCusStaus[i].cus_id = data[index].cus_id;
			 newCusStaus[i].cus_nickname = data[index].cus_nickname;			
		}
		createCusContent('not first',newCusStaus);
		
	}
}
function setAllMap(markers, map){
	for(var i = 0; i < markers.length; i++){
		markers[i].setMap(map);
	}
}
function getInfoArray2(data, type, info){
	if(type == 'order'){
		for(var index in info){
			cus_id[index] == info[index].cus_id;
			//nickname not exist!
			cus_nickname[index] == info[index].cus_nickname;
			cus_status[index] == info[index].status;
			cus_lat[index] == info[index].cus_lat;
			cus_lng[infex] == info[index].cus_lng;
		}
		switch(info){
		case 'cus_id' : 
			return cus_id;
			break;
		case 'cus_nickname' :
			return cus_nickname;
			break;
		case 'cus_status' :
			return status;
			break;
		case 'cus_lat' :
			return cus_lat;
			break;
		case 'cus_lng' : 
			return cus_lng;
			break;
		default :
			return info;
		}
		
	}

	
}